// 妖气


let projcet = JSON.parse(files.read("project.json"))

let storage = storages.create(projcet.packageName)

// 获取用户配置参数


let g_battle_failed = storage.get("bh_tansuo_is_fail")


// let g_waittime = {type: "edit", value: 5}


let thread = storage.get("bh_thread")
if (!thread) {
    thread = { type: 'spinner', value: 0 }
}


let ViewData = require("script/src/view")
let MapData = require("script/src/map")
let Funs = require("script/src/funs")


function Script() {
    runtime.loadDex("module/lib/scriptlib.dex")
    importClass(com.scriptlib.AnchorGraphicHelper)
    
    
    ViewData.call(this)
    MapData.call(this)
    Funs.call(this)

    
    // 当前this
    let temp_this = this
    // 脚本线程，暂不可用
    this.thread_obj = threads.start(function(){print("thread_init")})

    
    // 初始化
    this.__init__ = function () {
        print("script init")
        auto.waitFor()
        app.launchApp("阴阳师")
        sleep(5 * 1000)

        
        
        if(!requestScreenCapture()){
            toast("请求截图失败");
            exit();
        }

        
        print(AnchorGraphicHelper.Version())

        
        let helper = AnchorGraphicHelper.Create(runtime, 1920, 1080, 0, 0, device.height - 1, device.width - 1)
        if(helper == null)throw "scriptLib init fail";
        helper.ReInitialize()
        this.helper = helper
        print("script init success")
    }


    // 主函数
    this.main = function () {
        this.__init__()
        // 滑动次数
        let count_0 = 0
        let v_count = 0
        let f_count = 0
        
        while (1) {
            // print("main init")
            print("screenshot")
            this.helper.KeepScreen(true)

            this.__now_map__(function (mName) {
                print("now in the ", mName)
                let t = null, tt = null, t_sum = 0;
                switch (mName) {
                    case "庭院":
                        t = this.__search_view__(this.views.__viewData__.reganize_team)
                        if (t.x > 0 && t.y > 0) {
                            this.funs.click(t.x, t.y, 25)
                        } else {

                        }
                        break;
                    case "探索":
                        // 筛选攻击目标
                        print("探索")
                        break;
                    case "组队":
                        print("组队场景")
                        t = this.__search_view__(this.views.__viewData__.auto_matching)
                        if (t.x > 0 && t.y > 0) {
                            this.funs.click(t.x, t.y, 25)
                            
                        } else {

                        }
                        break;
                    case "战斗":
                        // 这里可以做一些干扰函数
                        if (random(0,33) == 1) {
                            print("干扰")
                            this.funs.swipe(random(100, device.height-100), random(100, device.width-100), random(100, device.height-100), random(100, device.width-100), random(800, 1600))
                        }


                        
                        t = this.__search_view__(this.views.__viewData__.prepare)
                        if (t.x > 0 && t.y > 0) {
                            this.funs.click(t.x, t.y, 25)
                        }
                        t = this.__search_view__(this.views.__viewData__.accelerate)
                        if (t.x > 0 && t.y > 0) {
                            this.funs.click(t.x, t.y, 25)
                        }

                        t = this.__search_view__(this.views.__viewData__.manual)
                        if (t.x > 0 && t.y > 0) {
                            this.funs.click(t.x, t.y, 25)
                        }
                        
                        break;
                    case "其他":
                        // 其他地图
                        print("其他地图")
                        t = this.__search_view__(this.views.__viewData__.ok)

                        if (t.x > 0 && t.y > 0) {
                            this.funs.click(t.x, t.y, 25)
                        }

                        t = this.__search_view__(this.views.__viewData__.fail)
                        if (t.x > 0 && t.y > 0) {
                            this.funs.click(t.x, t.y, 25)
                            f_count++
                        }

                        t = this.__search_view__(this.views.__viewData__.victory)
                        if (t.x > 0 && t.y > 0) {
                            this.funs.click(t.x, t.y, 25)
                            v_count++
                        }

                        t = this.__search_view__(this.views.__viewData__.jingong)
                        if (t.x > 0 && t.y > 0) {
                            this.funs.click(t.x, t.y, 25)
                            print("开始进攻")
                        }

                        t = this.__search_view__(this.views.__viewData__.battle_end)
                        if (t.x > 0 && t.y > 0) {
                            this.funs.click(t.x, t.y, 25)
                        }
                        
                        break;
                    default:

                        
                        break;

                }

            })
            sleep(500)
        }
    }
    
    /**
     * 搜索控件 第二个参数是范围
     * @param {*} viewobj 
     * @returns 
     */
    this.__search_view__ = function (viewobj, ran) {

        let is = {
            x: -1,
            y: -1
        }
        
        let viewNmae = viewobj.name //当前控件名称
        print("start search %s view", viewNmae)

        // 开始遍历views数据
        for (let i = 0;i < viewobj.dataArr.length;i++) {
            let nowView = viewobj.dataArr[i]
            let range = null
            if (ran) {
                range = this.helper.GetRange(ran[0], ran[1], ran[2], ran[3], ran[4])
            } else {
                print("nowView.range")
                
                print(nowView.range[0])
                range = this.helper.GetRange(nowView.range[0], nowView.range[1], nowView.range[2], nowView.range[3], nowView.range[4])
            }
            
            print(range)
                                                                        
            let desc = nowView.desc
            // 相似度
            let similar = nowView.similarity || this.views.similarity
            // 偏移
            let deviation = nowView.deviation || this.views.deviation

            let findColorArray = this.helper.GetFindColorArray(desc[0],desc[1],desc[2])
            is = this.helper.FindMultiColor(range[0], range[1], range[2], range[3], findColorArray, similar, deviation)
            if (is.x > 0 && is.y > 0) {
                break
            }
        }
        return is
    }
    

    
    /**
     * 遍历地图数据，
     * @param {*} check 回调函数
     */
    this.__now_map__ = function (check) {

        let is = false // 是否找到地图数据
        let mapName = "" // 当前地图名称
        // 遍历mapData数据
        for (let i = 0; i < this.map.__mapData__.length; i++) {
            let nowPath = this.map.__mapData__[i]
            mapName = nowPath.name // 所在地图名称
            // print("开始获取%s数据", mapName)
            
            if (nowPath == null) continue;

            // 遍历dataArr数据
            for (let j = 0; j < nowPath.dataArr.length; j++) {
                let nowPathData = nowPath.dataArr[j]
                if (nowPathData == null) continue;
                let CmpColorArray = this.helper.GetCmpColorArray(nowPathData.desc[0],nowPathData.desc[1],nowPathData.desc[2])
                
                // 相似度
                let similar = nowPathData.similarity || this.map.similarity
                // 偏移
                let deviation = nowPathData.deviation || this.map.deviation

                
                is = this.helper.CompareColorEx(CmpColorArray, similar, deviation)
                if (is) break;
            }
            if (is) break;
        }

        if (!is) {
            mapName = "其他"
        }
        check.call(this, mapName)

    }
}


let ScriptLib = new Script()

if (thread.value == 0) {
    print("开始运行脚本")
    ScriptLib.main()
    
}